package com.ruoyi.ldzlsb.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.ldzlsb.domain.SbEquipmentStatusLog;
import com.ruoyi.ldzlsb.service.ISbEquipmentStatusLogService;

/**
 * 设备状态日志控制器
 */
@RestController
@RequestMapping("/sb/status/log")
public class SbEquipmentStatusLogController extends BaseController {
    
    @Autowired
    private ISbEquipmentStatusLogService sbEquipmentStatusLogService;

    /**
     * 查询设备状态变更日志列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SbEquipmentStatusLog sbEquipmentStatusLog) {
        startPage();
        List<SbEquipmentStatusLog> list = sbEquipmentStatusLogService.selectSbEquipmentStatusLogList(sbEquipmentStatusLog);
        return getDataTable(list);
    }

    /**
     * 导出设备状态变更日志列表
     */
    @Log(title = "设备状态日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbEquipmentStatusLog sbEquipmentStatusLog) {
        List<SbEquipmentStatusLog> list = sbEquipmentStatusLogService.selectSbEquipmentStatusLogList(sbEquipmentStatusLog);
        ExcelUtil<SbEquipmentStatusLog> util = new ExcelUtil<>(SbEquipmentStatusLog.class);
        util.exportExcel(response, list, "设备状态变更日志");
    }

    /**
     * 获取设备状态变更日志详细信息
     */
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId) {
        return success(sbEquipmentStatusLogService.selectSbEquipmentStatusLogById(logId));
    }

    /**
     * 新增设备状态变更日志
     */
    @Log(title = "设备状态日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbEquipmentStatusLog sbEquipmentStatusLog) {
        return toAjax(sbEquipmentStatusLogService.insertSbEquipmentStatusLog(sbEquipmentStatusLog));
    }

    /**
     * 修改设备状态变更日志
     */
    @Log(title = "设备状态日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SbEquipmentStatusLog sbEquipmentStatusLog) {
        return toAjax(sbEquipmentStatusLogService.updateSbEquipmentStatusLog(sbEquipmentStatusLog));
    }

    /**
     * 删除设备状态变更日志
     */
    @Log(title = "设备状态日志", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds) {
        return toAjax(sbEquipmentStatusLogService.deleteSbEquipmentStatusLogByIds(logIds));
    }
    
    /**
     * 查询指定设备的状态变更历史
     */
    @GetMapping("/equipment/{equipmentId}")
    public AjaxResult getEquipmentStatusHistory(@PathVariable("equipmentId") Long equipmentId) {
        List<SbEquipmentStatusLog> list = sbEquipmentStatusLogService.selectSbEquipmentStatusLogsByEquipmentId(equipmentId);
        return success(list);
    }
    
    /**
     * 获取设备状态统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getEquipmentStatusStats() {
        return success(sbEquipmentStatusLogService.getEquipmentStatusStatistics());
    }
    
    /**
     * 获取年度设备状态变更统计
     */
    @GetMapping("/yearly-changes")
    public AjaxResult getYearlyStatusChanges(Integer year) {
        if (year == null) {
            // 如果未提供年份，默认使用当前年份
            year = java.time.Year.now().getValue();
        }
        return success(sbEquipmentStatusLogService.getEquipmentStatusChangeByMonth(year));
    }
} 